package com.byzk.p2p.admin.task;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.byzk.p2p.admin.base.util.DateUtils;
import com.byzk.p2p.admin.fuiou.controller.FuionAccountController;
import com.byzk.p2p.admin.user.dto.User;
import com.byzk.p2p.admin.user.dto.UserAccount;
import com.byzk.p2p.admin.user.service.UserAccountService;
import com.byzk.p2p.admin.user.service.UserService;
/**
 * 自动任务:每天凌晨自动检查，哪些已经开通三方账户的用户，其平台账户和金账户金额不一致
 * @author fdq
 *
 */

@Component
public class AutoCheckBanlance{
	
	@Autowired
	private UserService userService;
	@Autowired
	private UserAccountService accountService;
	
	@Autowired
	private FuionAccountController fuionAccountController ; 
	
	@Autowired
    private static Logger logger = Logger.getLogger(AutoCheckBanlance.class); //日志服务
	
	//每天凌晨2点自动检查，哪些已经开通三方账户的用户，其平台账户和金账户金额不一致
//	@Scheduled(cron = "0 0 2 * * ? ")
    public void taskCycle(){
	    logger.info("自动任务==============================开始时间："+DateUtils.getNowTimeString());
	    Map<String, Object> queryMap = new HashMap<String, Object>();
	    queryMap.put("fuionAccount", "Y");
	    List<User> userList=userService.listAll1(queryMap);
	    int i =0;
	    int n=0;//共有多少笔不一致的数据
	    String userName;
	    logger.info("开始执行自动检查，记录数据总条数("+userList.size()+")");
	    for (User user : userList) {
	    	userName = user.getUsername();
	    	i++;
			UserAccount account=accountService.getByUserId(user.getId());
			if(account == null){
				continue;
			}
			//查询金账户用户可用余额
			UserAccount accountFuion =fuionAccountController.getFuionAccountBalance(user);
			if(accountFuion == null){
				continue;
			}
			if(account.getTotalAmount().compareTo(accountFuion.getTotalAmount())!=0){
				logger.info("正在处理第"+i+"条数据,用户["+userName+"]的迅泊达账户总额为:["+account.getTotalAmount()+"],金账户账户总额为["+accountFuion.getTotalAmount()+"]");
			}
			if(account.getBalance().compareTo(accountFuion.getBalance())!=0){
				logger.info("正在处理第"+i+"条数据,用户["+userName+"]的迅泊达可用余额为:["+account.getBalance()+"],金账户可用余额为["+accountFuion.getBalance()+"]");
			}
			if(account.getTotalAmount().compareTo(accountFuion.getTotalAmount())!=0 || account.getBalance().compareTo(accountFuion.getBalance())!=0){
				n++;
			}
	    }
	    
	    logger.info("自动任务==============================结束时间："+DateUtils.getNowTimeString()+",不一致数据笔数："+n);			
    }
    
	 public static void main(String[] args) {
		 AutoCheckBanlance task=new AutoCheckBanlance();
		 task.taskCycle();
	}
}

